<?php
/**
* PHP version 5
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package Textbook
* @subpackage User
* @author Aljaz Srebrnic <a2piratesoft@gmail.com>;
* @author E. Pavletič
* @copyright 2009 a2PirateSoftware
* @license GPL License v3 <http://www.gnu.org/copyleft/gpl.html>
*/

require_once(ZNF::MODULES_DIR.'/Textbook/Form/AbstractTextbookForm.php');

class Textbook_Form_TextbookSearchForm extends Textbook_Form_AbstractTextbookForm {

  private $isbn = null;
  private $title = null;
  private $titleArray = null; // Tabela posameznih izrazov naslova (uporabljena pri iskanju učbenikov)
  private $authors = null;
  private $authorsArray = null; // Tabela posameznih avtorjev (uporabljena pri iskanju učbenikov)
  private $publisher = null;

  public function populate() {
    if (isset($_POST['isbn']) && (strlen(trim($_POST['isbn'])) > 0))
      $this->isbn = $this->normalizeISBN($_POST['isbn']);
        
    if (isset($_POST['title']) && (strlen(trim($_POST['title'])) > 0)) {
      $this->title = htmlspecialchars(addslashes(trim($_POST['title'])));
      // Postavi besede iz naslova v tabelo, ki je uporabljena pri iskanju učbenikov. Izraze krajše od treh znakov, izbriše
      $titleTerms = explode(' ', trim($_POST['title']));
      $this->titleArray = array();
      foreach ($titleTerms as $term) {
	$term = trim($term);
	if (strlen($term) > 2)
	  array_push($this->titleArray, htmlspecialchars(addslashes($term)));
      }
    }

    if (isset($_POST['authors']) && (strlen(trim($_POST['authors'])) > 0)) {
      $this->authors = htmlspecialchars(addslashes(trim($_POST['authors'])));
      // Postavi avtorje v tabelo, ki je uporabljena pri iskanju učbenikov. Izraze krajše od treh znakov, izbriše
      $authors = explode(' ', $_POST['authors']);
      $this->authorsArray = array();
      foreach ($authors as $author) {
	$author = trim($author);
	if (strlen($author) > 2)
	  array_push($this->authorsArray, htmlspecialchars(addslashes($author)));
      }
    }

    if (isset($_POST['publisher']) && (strlen(trim($_POST['publisher'])) > 0))
      $this->publisher = htmlspecialchars(addslashes(trim($_POST['publisher'])));
  }


  public function validate() {
    $errors = new ZNF_Action_ActionErrors();
    $translation =  new ZNF_Presentation_Translation('Textbook', $_SESSION['znf']['lang']);
        
    if (($this->getIsbn() == null) &&
	($this->getTitleArray() == null) &&
	($this->getAuthorsArray() == null) &&
	($this->getPublisher() == null))
      $errors->addError(1, $translation->get('errorNoSearchTerms'));
        
    return $errors;
  }


  public function getIsbn() {
    return $this->isbn;
  }

  public function getTitle() {
    return $this->title;
  }

  public function getTitleArray() {
    return $this->titleArray;
  }

  public function getAuthors() {
    return $this->authors;
  }

  public function getAuthorsArray() {
    return $this->authorsArray;
  }

  public function getPublisher() {
    return $this->publisher;
  }
}